package com.qf.mao.servlet.clazz;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.mao.entity.RespBean;
import com.qf.mao.entity.RespPageBean;
import com.qf.mao.service.ClazzService;
import com.qf.mao.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 啾咪小魔王
 * @date 2022/4/8 15:13
 * @since JDK 1.8
 */
@SuppressWarnings("all")
@WebServlet("/ClazzServlet")
public class ClazzServlet extends HttpServlet {
    ClazzService clazzService = new ClazzService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("page".equals(action)){
            request.getRequestDispatcher("/WEB-INF/jsp/clazzList.jsp").forward(request,response);
        }else if ("data".equals(action)){
           //获取年级ID
            String gid = request.getParameter("gid");
            //分页页码
            String page = request.getParameter("page");
            //每页查询多少条
            String rows = request.getParameter("rows");
            String sort = request.getParameter("sort");
            String order = request.getParameter("order");
            RespPageBean respPageBean = clazzService.getClazzByPage(gid, page, rows, sort, order);
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(new ObjectMapper().writeValueAsString(respPageBean));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clazzName = request.getParameter("clazzName");
        String gid = request.getParameter("gid");
        Integer result = clazzService.addClazz(clazzName, Integer.parseInt(gid));
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        RespBean respBean = null;
        if (result == CommonsUtils.SUCCESS_OK) {
            respBean = RespBean.respSuccess("添加成功");
        } else if (result == CommonsUtils.ERROR_NAME) {
            respBean = RespBean.respError("班级名重复，添加失败");
        }else {
            respBean = RespBean.respError("添加失败");
        }
        out.write(new ObjectMapper().writeValueAsString(respBean));
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        Integer result = clazzService.deleteClazzByGid(cid);
        RespBean respBean = null;
        if (result == CommonsUtils.SUCCESS_DELETE) {
            respBean = RespBean.respSuccess("删除成功");
        }else{
            respBean = RespBean.respError("删除失败");
        }
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(new ObjectMapper().writeValueAsString(respBean));
    }
}
